﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using System.ComponentModel;
using DevExpress.XtraEditors.Controls;
using System.Windows.Forms;
using DevExpress.XtraEditors.Registrator;
using System.Reflection;
using System.IO;

namespace ProgNetComponentsDx.Controls.Editors
{
    public class FolderDialogEdit : ButtonEdit
    {
        static FolderDialogEdit()
        {
            RepositoryItemFolderDialogEdit.Register();
        }
        [Browsable(false)]
        public override string EditorTypeName { get { return "FolderDialogEdit"; } }
        [Description("Gets an object containing properties, methods and events specific to the FolderDialogEdit control."), Category(CategoryName.Properties), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemFolderDialogEdit Properties { get { return base.Properties as RepositoryItemFolderDialogEdit; } }
        protected override void CreateRepositoryItem()
        {
            if (fProperties == null ||
                (fProperties as RepositoryItemFolderDialogEdit) == null)
            {
                this.fProperties = new RepositoryItemFolderDialogEdit();
                Properties.CreateDefaultButton();
                (this.fProperties as RepositoryItemFolderDialogEdit).SetOwner(this);
            }
        }
        public FolderDialogEdit()
            : base()
        {
            Properties.CreateDefaultButton();
        }

    }
    [UserRepositoryItem("Register")]
    public class RepositoryItemFolderDialogEdit : RepositoryItemButtonEdit
    {
        #region Register Repository
        static RepositoryItemFolderDialogEdit()
        {
            Register();
        }
        public static void Register()
        {
            EditorRegistrationInfo.Default.Editors.Add(
                new EditorClassInfo("FolderDialogEdit",
                                typeof(FolderDialogEdit),
                                typeof(RepositoryItemFolderDialogEdit),
                                typeof(DevExpress.XtraEditors.ViewInfo.ButtonEditViewInfo),
                                new DevExpress.XtraEditors.Drawing.ButtonEditPainter(),
                                true,
                                null));
        }
        #endregion

        #region CTOR
        public RepositoryItemFolderDialogEdit()
        {
            Buttons.CollectionChanged += new CollectionChangeEventHandler(Buttons_CollectionChanged);
            Description = "Folder";
            UseRootAsPath = true;
            UseParentBackground = true;
        }
        #endregion

        #region Private Members
        private OpenFolderDlg openD;
        private OpenFolderDlg OpenDialog
        {
            get
            {
                if (openD == null)
                {
                    openD = new OpenFolderDlg();
                }
                return openD;
            }
        }

        private FolderBrowserDialog FopenD;
        private FolderBrowserDialog FOpenDialog
        {
            get
            {
                if (FopenD == null)
                {
                    FopenD = new FolderBrowserDialog();
                    FopenD.Description = null;
                    FopenD.RootFolder = Environment.SpecialFolder.MyComputer;
                    FopenD.ShowNewFolderButton = false;
                }
                return FopenD;
            }
        }
        #endregion

        #region Public Properties
        //[DefaultValue(false)]
        //[Category(ProgNetComponents.Constants.Categories.FolderDialog)]
        //public bool ShowNewFolderButton
        //{
        //    get { return FOpenDialog.ShowNewFolderButton; }
        //    set
        //    {
        //        FOpenDialog.ShowNewFolderButton = value;
        //        OpenDialog.ShowNewFolderButton = value;
        //    }
        //}

        [DefaultValue(null)]
        [Category(ProgNetComponents.Constants.Categories.FolderDialog)]
        public string Description
        {
            get { return FOpenDialog.Description; }
            set
            {
                FOpenDialog.Description = value;
                OpenDialog.Description = value;
            }
        }

        [DefaultValue(Environment.SpecialFolder.MyComputer)]
        [Category(ProgNetComponents.Constants.Categories.FolderDialog)]
        public System.Environment.SpecialFolder RootFolder
        {
            get { return FOpenDialog.RootFolder; }
            set {FOpenDialog.RootFolder = value; }
        }

        [Category(ProgNetComponents.Constants.Categories.FolderDialog)]
        public bool UseRootAsPath { get; set; }

        [DefaultValue("")]
        [Category(ProgNetComponents.Constants.Categories.FolderDialog)]
        public string RootDir
        {
            get { return OpenDialog.RootDir; }
            set { OpenDialog.RootDir = value; }
        }

        [DefaultValue(null)]
        [Category(ProgNetComponents.Constants.Categories.FolderDialog)]
        public string RootName
        {
            get { return OpenDialog.RootName; }
            set { OpenDialog.RootName = value; }
        }
        #endregion

        #region Override
        [Browsable(false)]
        public override string EditorTypeName { get { return "FolderDialogEdit"; } }
        [Browsable(false)]
        public new FolderDialogEdit OwnerEdit { get { return base.OwnerEdit as FolderDialogEdit; } }
        
        public override void CreateDefaultButton()
        {
            Buttons.Clear();
            EditorButton btn = new EditorButton("DELETE", ButtonPredefines.Delete);
            btn.IsDefaultButton = false;
            Buttons.Add(btn);
            btn = new EditorButton("EDIT", ButtonPredefines.Ellipsis);
            btn.IsDefaultButton = true;
            Buttons.Add(btn);
            this.ButtonClick -= new ButtonPressedEventHandler(RepositoryItemFolderDialogEdit_ButtonClick);
            this.ButtonClick += new ButtonPressedEventHandler(RepositoryItemFolderDialogEdit_ButtonClick);
        }
        public override BaseEdit CreateEditor()
        {
            return new FolderDialogEdit();
        }
        #endregion

        #region Public Methods
        public void SetOwner(BaseEdit edit)
        {
            base.SetOwnerEdit(edit);
        }
       
        #endregion

        #region Private Events
        void RepositoryItemFolderDialogEdit_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            BaseEdit Edit = sender as BaseEdit;
            if (e.Button != null && !Edit.Properties.ReadOnly)
            {
                if (e.Button.Tag.AsString() == "DELETE")
                {
                    if (Edit != null)
                        Edit.EditValue = null;
                }
                else if (e.Button.Tag.AsString() == "EDIT")
                {
                    if (Edit != null)
                    {
                        string path = Edit.EditValue.AsString();
                        if (RootDir.IsNotNull() && UseRootAsPath)
                            path = path.XReplace("^%" + RootName + "%", RootDir);

                        if (RootDir.IsNotNull())
                        {
                            OpenDialog.SelectedPath = path;
                            if (OpenDialog.ShowDialog() == DialogResult.OK)
                            {
                                path = OpenDialog.SelectedPath;
                                if (UseRootAsPath)
                                    path = path.XReplace("^" + RootDir.XEscape(), "%" + RootName + "%");
                            }
                        }
                        else
                        {
                            FOpenDialog.SelectedPath = path;
                            if (FOpenDialog.ShowDialog() == DialogResult.OK)
                            {
                                path = FOpenDialog.SelectedPath;
                            }
                        }
                        Edit.EditValue = path;
                    }
                }
            }

        }
        void Buttons_CollectionChanged(object sender, CollectionChangeEventArgs e)
        {
            if (e.Action == CollectionChangeAction.Remove)
            {
                if ((e.Element as EditorButton).Tag.AsString() == "EDIT" ||
                    (e.Element as EditorButton).Tag.AsString() == "DEL")
                {
                    Buttons.Add(e.Element as EditorButton);
                }
            }
        }
        #endregion

    }
}
